package com.example.allshengmedical.experimentrun.activity;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.example.allshengmedical.util.FileManagerUtil;
import com.example.allshengmedical.util.LogUtil;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;

public class TempDBHelper {
	// final static String TempDBName = "TempDB.db";
	final static String TableName = "TempDB";
	final static String TempColumnName = "temp";
	final static String TempMode2Name = "temp2";
	final static String TempMode3Name = "temp3";
	public static final int VERSION = 1;

	// public static final String TABLENUMBER = "1";
	SQLiteDatabase database;
	File file;
	Context context;

	public TempDBHelper(Context ctx) {
		// TODO Auto-generated constructor stub
		File file = createDBFile();
		this.context = ctx;
		this.database = SQLiteDatabase.openOrCreateDatabase(file, null);
	}

	// public TempDBHelper(Context context, String name, CursorFactory factory,
	// int version) {
	// super(context, name, factory, version);
	// // TODO Auto-generated constructor stub
	// }
	//
	// public TempDBHelper(Context context) {
	// // TODO Auto-generated constructor stub
	//
	// super(context, TempDBName, null, VERSION);
	// }
	//
	// @Override
	// public void onCreate( ) {
	// // TODO Auto-generated method stub
	// file = createDBFile();
	// database = SQLiteDatabase.openOrCreateDatabase(file, null);
	// this.database = db;
	// StringBuilder sqlBuilder = new StringBuilder();
	// sqlBuilder.append("CREATE  TABLE  " + TableName + "  ( ");
	// sqlBuilder
	// .append("_id Integer primary key , temp TEXT , temp2 TEXT , temp3 TEXT)");
	// database.execSQL(sqlBuilder.toString());
	// }

	public File createDBFile() {
		FileManagerUtil fileManagerUtil = new FileManagerUtil();
		fileManagerUtil.createDB();
		File file = new File(Environment.getExternalStorageDirectory() + "/"
				+ "AllShengDB" + "/" + CommonSQLiteHelper.DATABASE_NAME);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return file;

	}

	public void deleteDBFile() {
		File file = new File(Environment.getExternalStorageDirectory() + "/"
				+ "AllShengDB" + "/" + CommonSQLiteHelper.DATABASE_NAME);
		if (file.exists()) {
			file.delete();
		}
	}

	/**
	 * @param tempColumnName
	 *            模块1
	 * @param tempMode2Name
	 *            模块2
	 * @param tempMode3Name
	 *            热盖
	 */
	public void add(String tempColumnName, String tempMode2Name,
			String tempMode3Name) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(TempColumnName, tempColumnName);
		contentValues.put(TempMode2Name, tempMode2Name);
		contentValues.put(TempMode3Name, tempMode3Name);
		// contentValues.put("temp2", 22 + "");
		database.insert(TempDBHelper.TableName, null, contentValues);
		contentValues.clear();
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void close() {
		file = createDBFile();
		database = SQLiteDatabase.openOrCreateDatabase(file, null);
		if (database != null)
			database.close();
	}

	public ArrayList<String> queryTempMode2Name() {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT " + TempMode2Name + " FROM "
				+ TempDBHelper.TableName, null);
		while (cursor.moveToNext()) {
			String string = cursor.getString(cursor
					.getColumnIndex(TempMode2Name));
			arrayList.add(string);
		}

		return arrayList;

	}

	public ArrayList<String> queryTempMode3Name() {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT " + TempMode3Name + " FROM "
				+ TempDBHelper.TableName, null);
		while (cursor.moveToNext()) {
			String string = cursor.getString(cursor
					.getColumnIndex(TempMode3Name));
			arrayList.add(string);
		}
		// tempArrayList = dbHelper.query();

		return arrayList;

	}

	public ArrayList<String> query() {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT " + TempColumnName + " FROM "
				+ TempDBHelper.TableName, null);
		while (cursor.moveToNext()) {
			String string = cursor.getString(cursor
					.getColumnIndex(TempColumnName));
			arrayList.add(string);
		}
		// tempArrayList = dbHelper.query();

		return arrayList;

	}

	public Cursor queryTheCursor() {
		Cursor c = database.rawQuery("SELECT * FROM " + TableName, null);
		return c;
	}

}
